package com.pl.action;

import com.alibaba.fastjson.JSON;
import com.pl.bean.ManagePageBean;
import com.pl.bean.ManageRoleBean;
import com.pl.bean.ManageUserBean;
import com.pl.bean.OauthClientDetailsBean;
import com.pl.bean.OauthUserInfo;
import com.pl.bean.ResultData;
import com.pl.service.ManagePageService;
import com.pl.service.ManageRoleService;
import com.pl.service.ManageUserService;
import com.pl.service.OauthClientDetailsService;
import com.pl.util.CommonUtil;
import com.pl.util.PasswordUtil;
import com.pl.util.ResultDataUtil;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Oauth2接口
 */
@Controller
@RequestMapping(value="userInfo")
public class PortalUserInfoAction {

	private static Logger log = LogManager.getLogger();
	
	@Resource(name="manageUserService")
	private ManageUserService manageUserService;
	
	@Resource(name="managePageService")
	private ManagePageService managePageService;
	
	@Resource(name="manageRoleService")
	private ManageRoleService manageRoleService;
	
	@Autowired
	private OauthClientDetailsService oauthClientDetailsService;
	
	/**
	  *  获得用户  
	 */
	@RequestMapping(value="")
	@ResponseBody
	public ResultData<OauthUserInfo> user(OAuth2Authentication user) {
		
		log.info("获得用户 START Principal={}", JSON.toJSONString(user));
		
		ManageUserBean manageUserBean = manageUserService.getManageUser(user);
		log.info("获得用户 DO-1 manageUserBean={}", JSON.toJSONString(manageUserBean));
		
		OauthUserInfo userInfo = new OauthUserInfo();
		userInfo.setUserId(manageUserBean.getId());
		userInfo.setUserName(user.getName());
		userInfo.setRealName(manageUserBean.getReal_name());
		userInfo.setUser(user);
		userInfo.setPageList(managePageService.getManagePageByUser(manageUserBean.getId(), user.getOAuth2Request().getClientId()));
		
		log.info("获得用户 END userInfo={}", JSON.toJSONString(userInfo));
        return ResultDataUtil.ok(userInfo);
    }
	
	/**
	  *  角色列表
	 */
	@RequestMapping(value="rolePage")
	@ResponseBody
	public ResultData<List<ManagePageBean>> rolePage(OAuth2Authentication user){
		
		log.info("获得用户可以访问的列表 START Principal={}", JSON.toJSONString(user));
		
		ManageUserBean manageUserBean = manageUserService.getManageUser(user);
		log.info("获得用户可以访问的列表 DO-1 manageUserBean={}", JSON.toJSONString(manageUserBean));
		
		List<ManagePageBean> pageList = managePageService.getManagePageByUser(manageUserBean.getId(), user.getOAuth2Request().getClientId());
		log.info("获得用户可以访问的列表 END pageList={}", JSON.toJSONString(pageList));
		
		return ResultDataUtil.ok(pageList);
	}
	
	/**
	  *  修改密码
	 */
	@RequestMapping(value="editPassword")
	@ResponseBody
	public ResultData<String> editPassword(OAuth2Authentication user, String oldPassword, String newPassword ){
		
		log.info("修改密码 START Principal={}", JSON.toJSONString(user));
		
		ManageUserBean bean = manageUserService.getManageUser(user);
		log.info("修改密码 DO-1 manageUserBean={}", JSON.toJSONString(bean));
		
		//判断旧密码是否正确
		if(!PasswordUtil.matches(bean.getPassword(), oldPassword)){
			return ResultDataUtil.fail("旧密码不正确");
		}
		
		//保存新密码
		long beanId = bean.getId();
		bean = new ManageUserBean();
		
		bean.setId(beanId);
		bean.setOperate_id(bean.getId());
		bean.setUpdate_time(CommonUtil.getCurrentDatetime());
		bean.setPassword(PasswordUtil.getEncoderPassword(newPassword));
		
		manageUserService.updateManageUser(bean);
		
		log.info("修改密码 END");
		
		return ResultDataUtil.ok();
	}
	
	/**
	  *  获得用户可以访问的客户端
	 */
	@RequestMapping(value="myClient")
	@ResponseBody
	public ResultData<List<Map<String, String>>> myClient(OAuth2Authentication user){
		
		log.info("获得用户可以访问的客户端 START Principal={}", JSON.toJSONString(user));
		
		ManageUserBean manageUserBean = manageUserService.getManageUser(user);
		log.info("获得用户可以访问的客户端 DO-1 manageUserBean={}", JSON.toJSONString(manageUserBean));
		
		OauthClientDetailsBean param = new OauthClientDetailsBean();
		param.setParamUserId(manageUserBean.getId());
		List<OauthClientDetailsBean> list = oauthClientDetailsService.getOauthClientDetails(param);
		
		List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
		list.forEach((item) -> {
			Map<String, String> map = new HashMap<String, String>();
			map.put("clientId", item.getClientId());
			map.put("clientName", item.getClientName());
			map.put("webServerRedirectUri", item.getWebServerRedirectUri());
			
			listMap.add(map);
		});
		
		log.info("获得用户可以访问的客户端 END list={}", JSON.toJSONString(listMap));
		
		return ResultDataUtil.ok(listMap);
	}
	
	/**
	  *  获得当前登录子系统下，某个角色下的所有用户
	 */
	@RequestMapping(value="getUserByRole")
	@ResponseBody
	public ResultData<List<ManageUserBean>> getUserByRole(OAuth2Authentication user, String roleCode){
		
		log.info("获得用户 START roleCode={} Principal={}", roleCode, JSON.toJSONString(user));
		
		//获得角色id
		ManageRoleBean roleBean = manageRoleService.getManageRole(user.getOAuth2Request().getClientId(), roleCode);
		log.info("获得用户 DO-1 roleBean={}", JSON.toJSONString(roleBean));
		
		ManageUserBean param = new ManageUserBean();
		param.setParam_role_id(roleBean.getId());
		List<ManageUserBean> list = manageUserService.getManageUser(param);
		log.info("获得用户 END list={}", JSON.toJSONString(list));
		
		return ResultDataUtil.ok(list);
	}
}
